﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public static class UserDAO
    {
        public static DataTable getAllUser()
        {
            string strQuery = " Select * from UserMail where StatusUser= true";
            var dt = DataProvider.getTable(strQuery);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            return dt;
        }
      
        public static DataTable checkLogin(string username, string pass)
        {
            string strQuery = " Select * from UserMail where Username ='" + username + "' and PassUser = '" + pass + "'";
            var dt = DataProvider.getTable(strQuery);
            if (dt==null || dt.Rows.Count==0)
            {
                return null;
            }
            
            return dt;
        }
        public static DataTable getUserLast()
        {
            string strQuery = " Select top 1 * from UserMail  order by Id DESC";
            var dt = DataProvider.getTable(strQuery);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            return dt;
        }
        public static DataTable getUserByid(int id)
        {
            string strQuery = " Select * from UserMail where Id =" + id+"";
            var dt = DataProvider.getTable(strQuery);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            return dt;
        }
        public static DataTable getUserByName(string username)
        {
            string strQuery = " Select * from UserMail where Username ='" + username + "'";
            var dt = DataProvider.getTable(strQuery);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            return dt;
        }
        public static bool insertUser(UserDTO user)
        {
            string query = "insert into UserMail (Username,PassUser,RuleUser,StatusUser) values ('" +
                user.Username+"','"+user.PassWord+"',"+user.RuleUser+","+user.StatusUser+")";
            return DataProvider.runQuery(query);
        }
        public static bool updateUser(UserDTO user)
        {
            string query = "update UserMail set Username='" + user.Username + "',PassUser='" + user.PassWord + "',RuleUser=" + user.RuleUser + ",StatusUser=" + user.StatusUser + " where Id="+user.Id;
            return DataProvider.runQuery(query);
        }
        public static bool deleteUser(int id)
        {
            string query = "update UserMail set StatusUser= false where Id=" + id;
            return DataProvider.runQuery(query);
        }
    }
}
